﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Logic;
using TrainingsApp.Model;
using TrainingsApp.Helper;

namespace TrainingsApp.IPADApp.Permission
{
    public partial class RoleFunction : PageBase
    {
     

        private static List<T_FUNCTION_Entity> FuncList = null;
        private static List<T_ROLE_FUNCTION_Entity> RoleFuncList = null;
        public Int32 RoleId
        {
            get { return int.Parse(Request.QueryString["BizCode"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lbRole.Text = Server.UrlDecode(Request.QueryString["name"]);
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                this.TreeView.Nodes.Clear();

                FuncList = Sys_Permission_Logic.T_FUNCTION_GetAllFunction();

                RoleFuncList = Sys_Permission_Logic.T_Role_Function_ListByRoleId(RoleId);

                TreeViewMethod.LoadTree<T_FUNCTION_Entity>(this.TreeView, "0", FuncList, RoleFuncList.Select(a => a.FUNCTIONID.ToString()).ToList(), "ID", "FUNCTIONNAME", "PARENTID");
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<T_ROLE_FUNCTION_Entity> list = new List<T_ROLE_FUNCTION_Entity>();
            int val = 0;
            foreach (TreeNode node in this.TreeView.CheckedNodes)
            {
                if (!int.TryParse(node.Value, out val))
                {
                    continue;
                }
                list.Add(new T_ROLE_FUNCTION_Entity() { ROLEID = RoleId, FUNCTIONID = val });
            }

            try
            {
                Sys_Permission_Logic.T_Role_Function_Update(RoleId, list);

                MessageBox.Show("操作成功！", MessageBoxIcon.Sucess);
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(UrlReferrer);
        }

    }
}